<?php
JunctionFileCabinet::using("Junction_Mapping_Association");
JunctionFileCabinet::using("Junction_Mapping_Core");
JunctionFileCabinet::using("Junction_Mapping_References");
JunctionFileCabinet::using("Junction_Mapping_Table");
JunctionFileCabinet::using("Test_Mock_Coupon");
JunctionFileCabinet::using("Test_Mock_Email");
JunctionFileCabinet::using("Test_Mock_User");
JunctionFileCabinet::package("Junction_Clause");

// TODO fix the clauses so they are translated from property to column
class Test_Junction_Mapping_Core extends UnitTestCase {
	
	/**
	 * @var Junction_Mapping_Core
	 */
	private $_mapping;
	
	public function testConstruct() {
		$this->_mapping = Junction_Core_Factory::construct("Test_Mock_Coupon");
	}
	
	public function testGetObject() {
		$coupon = $this->_mapping->getObjectFrom(array("coupon_id" => 1, "title" => "foobar", "value" => 32));
		$this->assertTrue($coupon->getId() == 1);
		$this->assertTrue($coupon->getTitle() == "foobar");
		$this->assertTrue($coupon->getValue() == 32);
	}
	
	public function testLoad() {
		$coupons = $this->_mapping->loadWhere(Junction_Clause_Null::construct());
		$collect = array();
		foreach ($coupons as $coupon) {
			$collect[] = $coupon;
		}
		$this->assertTrue(count($collect) > 0);
		$this->assertTrue($collect[0]->getId() == 1);
		$this->assertTrue($collect[0]->getTitle() == "foobar");
		$this->assertTrue($collect[0]->getValue() == 32);
		
		// TODO fix the clause's so they get translated properly.
		// TODO fix the iterator so that it can be used outside of the for loop construction
		$idIsOne = new Junction_Clause_Where("coupon_id = ?");
		$idIsOne->bind(0, 1);
		$coupons = $this->_mapping->loadWhere($idIsOne);
		$int = 0;
		foreach ($coupons as $coupon) { // this is going to execute once
			$this->assertTrue($coupon->getId() == 1);
			$this->assertTrue($coupon->getTitle() == "foobar");
			$this->assertTrue($coupon->getValue() == 32);
			$this->assertTrue($int == 0);
			$int++;
		}
	}
	
	public function testSave() {
		$coupon = new Test_Mock_Coupon();
		$coupon->setTitle("barfoo");
		$coupon->setValue(49.99);
		$this->assertTrue($this->_mapping->insert($coupon));
		$this->assertTrue($coupon->getId() > 0);
		
		$coupon->setValue(29.99);
		$this->assertTrue($this->_mapping->update($coupon));
		
		$coupon = new Junction_Core_Decorator("Test_Mock_Coupon");
		$coupon->setTitle("foobar");
		$coupon->setValue(20);
		$this->assertTrue($this->_mapping->insert($coupon));
		$this->assertTrue($coupon->getId() > 0);
	}
	
	public function testDelete() {
		$valueIsLow = new Junction_Clause_Where("value < ?");
		$valueIsLow->bind(0, 30);
		$this->assertTrue($this->_mapping->deleteWhere($valueIsLow) > 0);
	}
	
	public function testUser() {
		$userMapping = Junction_Core_Factory::construct("Test_Mock_User");
		$user = new Test_Mock_User();
		
		$user->setName("JohnDoe");
		$user->setPassword("password");
		$user->setCreated(time());
		
		$this->assertTrue($userMapping->insert($user));
		
		$this->assertTrue($user->getId());
		
		$user->setPassword("newPassword");
		
		$this->assertTrue($userMapping->update($user));
		
		$idIsOne = new Junction_Clause_Where("user_id = ?");
		$idIsOne->bind(0, 1);
		$users = $userMapping->loadWhere($idIsOne);
		
		foreach ($users as $aUser) {
			$user = $aUser;
		}
		
		$emailMapping = Junction_Core_Factory::construct("Test_Mock_Email");

		$email = new Test_Mock_Email();
		$email->setUser($user->getId());
		$email->setAddress("johndoe@mail.com");
		
		$this->assertTrue($emailMapping->insert($email));
		
		$this->assertTrue($email->getId() > 0);
		
		$emails = $user->getEmails();
		$mails = array();
		foreach ($emails as $email) {
			$mails[] = $email;
		}
		$this->assertTrue(count($mails) > 0);
		
		$emailIsJohnDoe = new Junction_Clause_Where("address = ?");
		$emailIsJohnDoe->bind(0, "johndoe@mail.com");
		$this->assertTrue($emailMapping->deleteWhere($emailIsJohnDoe) > 0);
		
		$nameIsFoobarOrJohn = new Junction_Clause_Where("name = ? || name = ?");
		$nameIsFoobarOrJohn->bind(0, "foobar");
		$nameIsFoobarOrJohn->bind(1, "JohnDoe");
		$this->assertTrue($userMapping->deleteWhere($nameIsFoobarOrJohn) > 0);
	}
	
}
?>